/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hoja15_3;

import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutput;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.Stack;
import java.util.logging.Level;
import java.util.logging.Logger;

/**
 *
 * @author Saul
 */
public class Modulo {

    private String nombreModulo;
    private ArrayList<Alumno> alumnos;
    private Alumno atemp;
    private int cargados;

    public Modulo(String nombreModulo) {
        this.alumnos = new ArrayList();
        this.nombreModulo = nombreModulo;
        this.cargados = 0;
    }

    public void añadir() {
        this.alumnos.add(new Alumno());
    }

    public void mosInv() {
        for (int i = this.alumnos.size() - 1; i >= 0; i--) {
            System.out.println(this.alumnos.get(i).toString());
        }
    }
    
    /**
     * @exception IOException No encuentra archivo
     */

    public void imp() {
        File fa, fs;
        BufferedWriter bwA, bwS;
        try {
            fa = new File("aprobados.txt");
            fs = new File("suspensos.txt");
            bwA = new BufferedWriter(new FileWriter(fa));
            bwS = new BufferedWriter(new FileWriter(fs));
            bwA.write(this.nombreModulo + "\n\n");
            bwS.write(this.nombreModulo + "\n\n");
            for (int i = 0; i < this.alumnos.size(); i++) {
                if (this.alumnos.get(i).getNota() >= 5) {
                    bwA.write(this.alumnos.get(i).toString2() + "\n");
                } else {
                    bwS.write(this.alumnos.get(i).toString2() + "\n");
                }
            }
            bwA.close();
            bwS.close();
        } catch (IOException ex) {
            System.out.println("Error");
        }
    }
/**
 * @since 2.0
 **/
    public void inicio() {
        
        File f = new File("datObject.dat");
        ObjectInputStream ois = null;
        boolean fin = false;
        try {
            ois = new ObjectInputStream(new FileInputStream(f));
            do {
                this.atemp = (Alumno) ois.readObject();
                this.alumnos.add(this.atemp);
                cargados++;
            } while (!fin);
            ois.close();
        } catch (EOFException ex1) {
            fin = true;
        } catch (IOException ex) {
            if (!f.exists()) {
                System.out.println("El fichero no existe, no se han cargado datos");
            } else {
                System.out.println("Error al abrir");
            }
        } catch (ClassNotFoundException ex) {
            Logger.getLogger(Modulo.class.getName()).log(Level.SEVERE, null, ex);
        }
    }

    public void fin() {
        File f = new File("datObject.dat");
        ObjectOutputStream oos = null;
        try {
            if (!f.exists()) {
                oos = new ObjectOutputStream(new FileOutputStream(f, true));
            } else {
                oos = new MiObjectOutputStream(new FileOutputStream(f, true));
            }
            for (int i = this.cargados; i < this.alumnos.size(); i++) {
                oos.writeObject(this.alumnos.get(i));
            }
        } catch (IOException ex) {
            System.out.println("Problemas al grabar");
        }
    }
}
